package com.LensBlaster;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

/**
 * Allows you to upload photos to Flickr using the Share 
 * Button in the Camera and Pictures applications in Android.
 */
public class ShareButton extends Activity {

	private final int ACTIVITY_UPLOAD = 1;
	private Uri mImageURI = null;

	/**
	 * Gets image from Intent.ACTION_SEND, and uploads it to Flickr.
	 */
	@Override
	public void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		String action = intent.getAction();

		if (Intent.ACTION_SEND.equals(action)) {

			mImageURI = (Uri) intent.getParcelableExtra(intent.EXTRA_STREAM);
			ContentResolver contentRes = getContentResolver();

			Cursor c = contentRes.query(mImageURI, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
			c.moveToFirst();
			int colIdx = c.getColumnIndex(MediaStore.Images.Media.DATA);
			String path = c.getString(colIdx);
			android.util.Log.d("lens",path);
			c.close();

			Intent i = new Intent(this, com.LensBlaster.upload.Flickr.Upload.class);
			i.setData(Uri.parse(path));
			startActivityForResult(i,ACTIVITY_UPLOAD);

		}
	}

	/**
	 * Notifies user when pictures is successfully uploaded.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (intent!=null) {
			super.onActivityResult(requestCode, resultCode, intent);

			switch(requestCode) {
			case ACTIVITY_UPLOAD:

				Toast t = Toast.makeText(this, "Photo uploaded.", 3);
				t.show();
				finish();
				break;

			}
		}
	}

}
